/**
 * @file     sumOfLeftLeaves.js
 * @brief    [404. 左叶子之和](https://leetcode.cn/problems/sum-of-left-leaves/description/)
 * @author   Zhu
 * @date     2023-02-12 18:44
 */

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumOfLeftLeaves = function (root) {
    let sum = 0;
    let stack = [root];
    while (stack.length) {
        let cur = stack.pop();
        if (cur.left) {
            if (cur.left.left === null && cur.left.right === null) {
                sum += cur.left.val;
            } else stack.push(cur.left);
        }
        if (cur.right) stack.push(cur.right);
    }

    return sum;
};
